package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by chenzhengwen on 2015/9/21.
 */
public class MovementTxtView extends View {
    private Paint mPaint;
    private int mTxtWidth;
    private int mTxtHeight;
    private int mViewWidth;
    private int mViewHeight;
    private int mPadding;
    private String mText;
    private int mTxtSize = 16;

    private float locX;
    private float locY;

    public MovementTxtView(Context context) {
        this(context, null);
    }

    public MovementTxtView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MovementTxtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mText = "Hello World";
        mPadding = (int)(3 * this.getResources().getDisplayMetrics().density);
        mPaint = new Paint();
        mPaint.setTextSize(mTxtSize * this.getResources().getDisplayMetrics().density);
        mPaint.setColor(Color.BLACK);
       /* mTxtWidth = (int)mPaint.measureText(mText);
        mTxtHeight = (int)(mPaint.ascent() + mPaint.descent());

        mViewWidth = mTxtWidth + (mPadding << 2);
        mViewHeight = mTxtHeight + (mPadding << 2);*/
    }

   /* @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mViewWidth, height + (mPadding << 2));
    }*/

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        canvas.drawARGB(255, 100, 100, 100);

        canvas.drawText(mText, (mViewWidth - mTxtWidth) / 2, (mViewHeight - mTxtHeight) / 2, mPaint);*/
        canvas.drawARGB(255, 100, 100, 100);
        canvas.drawText(mText, locX, locY, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        locX = event.getX();
        locY = event.getY();

        invalidate();
        return true;
    }
}
